﻿// TODO: LocalExecuteAsync unknown 

using System;
using System.Diagnostics;
using System.Reflection;
using System.Runtime;
using System.Threading.Tasks;
using Extention.ENode.Rpc.Runtime.Communally.Entitys;
using Extention.ENode.Rpc.Runtime.Communally.Entitys.Messages;
using Extention.ENode.Rpc.Transport;
using Microsoft.Extensions.Logging;

namespace Extention.ENode.Rpc.Runtime.Server.Impl
{
    public class DefaultServiceExecutor : IServiceExecutor
    {
        private readonly IServiceEntryLocate _serviceEntryLocate;
        private readonly ILogger<DefaultServiceExecutor> _logger;

        public DefaultServiceExecutor(IServiceEntryLocate serviceEntryLocate, ILogger<DefaultServiceExecutor> logger)
        {
            _serviceEntryLocate = serviceEntryLocate;
            _logger = logger;
        }

        /// <summary>
        /// 执行消息转换后的服务
        /// </summary>
        /// <param name="sender">消息发送者</param>
        /// <param name="message">调用消息</param>
        public async Task ExecuteAsync(IMessageSender sender, TransportMessage message)
        {
            try
            {
                // _logger.LogInformation("请求类型:" + message.ContentType);
                // _logger.LogInformation("请求ID:" + message.Id);
                if (!message.IsInvokeMessage()) return;

                RemoteInvokeMessage remoteInvokeMessage;
                try
                {
                    remoteInvokeMessage = message.GetContent<RemoteInvokeMessage>();
                }
                catch (Exception exception)
                {
                    _logger.LogError("将接收到的消息反序列化成 TransportMessage<RemoteInvokeMessage> 时发送了错误。", exception);
                    return;
                }

                // 定位远程调用的消息服务实体
                var entry = _serviceEntryLocate.Locate(remoteInvokeMessage);
                if (entry == null)
                {
                    if (_logger.IsEnabled(LogLevel.Error))
                        _logger.LogError($"根据服务Id：{remoteInvokeMessage.ServiceId}，找不到服务条目");
                    return;
                }
                //  _logger.LogInformation("准备执行本地逻辑");
                var resultMessage = new RemoteInvokeResultMessage();
                //GetMemoryInfo();
                // 是否需要等待执行
                if (entry.Descriptor.WaitExecution())
                {
                    //  _logger.LogInformation("准备执行本地逻辑1");
                    //执行本地代码
                    await LocalExecuteAsync(entry, remoteInvokeMessage, resultMessage);
                    //向客户端发送调用结果
                    await SendRemoteInvokeResult(sender, message.Id, resultMessage);
                }
                else
                {
                    // _logger.LogInformation("准备执行本地逻辑2");
                    //通知客户端已接收到消息
                    await SendRemoteInvokeResult(sender, message.Id, resultMessage);
                    //确保新起一个线程执行，不堵塞当前线程
                    await Task.Factory.StartNew(async () =>
                        {
                            //执行本地代码
                            await LocalExecuteAsync(entry, remoteInvokeMessage, resultMessage);

                        },
                        TaskCreationOptions.LongRunning);
                }
            }
            catch (Exception)
            {

                throw;
            }

        }
        public void GetMemoryInfo()
        {
            Process[] p = Process.GetProcesses();//获取进程信息

            long WorkingSet64 = 0;
            string info = "";
            foreach (Process pr in p)
            {
                WorkingSet64 += pr.WorkingSet64 / 1024 / 1024;
                info += pr.ProcessName + "内存WorkingSet64：-----------" + (pr.WorkingSet64 / 1024 / 1024).ToString() + "M\r\n";
            }
            info = "-------------------------------------------------------------------------------------\r\n" + info;//得到进程内存
            info = "内存WorkingSet64：-----------" + (WorkingSet64).ToString() + "M\r\n" + info;//得到进程内存
            info = "是否为ServerGC：-----------" + (GCSettings.IsServerGC).ToString() + "\r\n" + info;//得到进程内存
            info = "-------------------------------总计--------------------------------------------------\r\n" + info;//得到进程内存
            _logger.LogInformation(info);
        }
        private async Task LocalExecuteAsync(ServiceEntity entry, RemoteInvokeMessage remoteInvokeMessage,
            RemoteInvokeResultMessage resultMessage)
        {
            try
            {
                var result = await entry.Func(remoteInvokeMessage.Parameters);
                var task = result as Task;

                if (task == null)
                {
                    resultMessage.Result = result;
                }
                else
                {
                    if (!task.IsCompletedSuccessfully)
                        await task;
                    var taskType = task.GetType().GetTypeInfo();
                    if (taskType.IsGenericType)
                        resultMessage.Result = taskType.GetProperty("Result").GetValue(task);



                }


            }
            catch (Exception exception)
            {
                if (_logger.IsEnabled(LogLevel.Error))
                    _logger.LogError("执行本地逻辑时候发生了错误", exception);
                resultMessage.ExceptionMessage = GetExceptionMessage(exception);

            }
        }

        private async Task SendRemoteInvokeResult(IMessageSender sender, string messageId,
            RemoteInvokeResultMessage resultMessage)
        {
            try
            {
                _logger.LogInformation("准备发送响应消息");
                await sender.SendAndFlushAsync(TransportMessage.CreateInvokeResultMessage(messageId, resultMessage));
                _logger.LogInformation("响应消息发送成功");
            }
            catch (Exception exception)
            {
                if (_logger.IsEnabled(LogLevel.Error))
                    _logger.LogError("发送响应消息时候发生了异常。{0}", exception);
            }
        }

        private static string GetExceptionMessage(Exception exception)
        {
            if (exception == null)
                return string.Empty;

            var message = exception.Message;
            if (exception.InnerException != null)
            {
                message += "|InnerException:" + GetExceptionMessage(exception.InnerException);
            }

            return message;
        }


    }
}